import { request } from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    beforeClose: {},
    showSign: false,
    showMessage: '',
    medalData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      beforeClose: (action) =>
        this.onBeforeClose(action)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let app = getApp()
    app.getCurrentTabbar(2, this)
    this.getUserInfo()
    this.getMedalList()
    this.getIsShowTeam()
  },

  share() {
    wx.navigateTo({
      url:
        '/packageMy/share/share?user=' +
        JSON.stringify(this.data.userInfo)
    })
  },

  getIsShowTeam() {
    request({
      url: '/api/cfg/config/queryByType/MINI'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          showMessage: resdata.data[0].value
        })
      } else {
        wx.showToast({
          title: resdata.message
        })
      }
    })
  },

  getUserInfo() {
    request({
      url: '/mini/api/mine/getUserInfo'
    })
      .then((resdata) => {
        if (resdata.code == 200) {
          this.setData({
            userInfo: resdata.data
          })
        } else {
          this.setData({
            userInfo: {}
          })
        }
      })
      .catch((err) => {
        console.log(err)
      })
  },

  getMedalList() {
    request({
      url: '/mini/api/mine/queryAchievementList',
      data: {
        userId: Number(this.data.userId)
      },
      method: 'POST'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          medalData: resdata.data
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  onBeforeClose: function (action) {
    let _this = this
    return new Promise((resolve) => {
      if (action === 'confirm') {
        request({
          url: '/mini/api/mine/payVip',
          method: 'POST'
        }).then((resdata) => {
          if (resdata.code == 200) {
            wx.showToast({
              title: '成功'
            })
            _this.getUserInfo()
            resolve(true)
          } else {
            wx.showToast({
              title: resdata.message,
              icon: 'none'
            })
            resolve(false)
          }
        })
      } else {
        this.onClose()
        resolve(false)
      }
    })
  },

  becomeVip() {
    this.setData({
      showSign: true
    })
  },

  onClose() {
    this.setData({
      showSign: false
    })
  },

  toUserInfo() {
    wx.navigateTo({
      url:
        '/packageMy/userInfo/userInfo?userInfo=' +
        JSON.stringify(this.data.userInfo)
    })
  },
  toRechargeList() {
    wx.navigateTo({
      url:
        '/packageMy/voucherCenter/voucherCenter'
    })
  },
  toMyCoupon() {
    wx.navigateTo({
      url: '/packageMy/myCoupon/myCoupon'
    })
  },
  toOrderList() {
    wx.navigateTo({
      url: '/packageMy/order/order'
    })
  },
  contactCustomerService() {
    const app = getApp()
    if (
      app.globalData.userInfo.unionId == null ||
      app.globalData.userInfo.unionId.length <= 0
    ) {
      wx.showToast({
        title:
          '用户当前权限不足，需要获取所有成就',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/packageMy/chatSession/chatSession'
    })
  },

  toMyActivity() {
    wx.navigateTo({
      url: '/packageMy/myActivity/myActivity'
    })
  },
  toMyCurriculum() {
    if (this.data.userInfo.role == 'TEACHER') {
      wx.navigateTo({
        url:
          '/packageMy/myCurriculum/myCurriculum'
      })
    }
  },
  toMyCompetition() {
    if (
      this.data.userInfo.role == 'TEACHER' ||
      this.data.userInfo.role == 'GROUPLEADER'
    ) {
      wx.navigateTo({
        url:
          '/packageMy/myCompetition/myCompetition'
      })
    }
  },
  toMyWorks() {
    if (this.data.userInfo.role == 'TEACHER') {
      wx.navigateTo({
        url: '/packageMy/myWorks/myWorks'
      })
    }
  },

  toPersonalHomepage() {
    wx.navigateTo({
      url:
        '/packageMy/personalHomepage/personalHomepage?from=' +
        'my&userId=' +
        this.data.userInfo.userId
    })
  },

  toMedal() {
    wx.navigateTo({
      url:
        '/packageMy/medal/medal?userId=' +
        this.data.userInfo.userId
    })
  },

  toFollowList() {
    wx.navigateTo({
      url:
        '/packageMy/followList/followList?id=' +
        this.data.userInfo.userId
    })
  },

  toFansList() {
    wx.navigateTo({
      url:
        '/packageMy/fansList/fansList?id=' +
        this.data.userInfo.userId
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
